home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: tada@athena.mit.edu (Michael J Zehr)
- Newsgroups: comp.lang.c.moderated,comp.std.c
- Subject: 'h' modifier in printf
- Date: 13 Mar 1996 20:24:44 -0600
- Organization: Massachusetts Institute of Technology
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4i801c$455@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
-
-
- I was recently asked a question about printf whose answer I couldn't
- determine by reading K&R2 (and alas the company doesn't have a copy of
- the standard to refer to).
-
- The "h" modifier says the corresponding argument will be printed as a
- short or unsigned short.
-
- So, given:
-
- short s;
- printf("%d", s);
- printf("%hd", s);
-
- (Assuming of course that s has been initialized at some point.)
-
- Can these two ever be different? I'm aware of course that the short is
- widened to an int during the function call, but this preserves the
- value.
-
- (A totally non-relevant piece of information is that on at least one
- platform there is never a difference between these two for any value of
- s from SHRT_MIN to SHRT_MAX. But that doesn't answer the question.)
-
- This is the main question I'm interested, but as a followup, if these
- always result in the same output, why is the 'h' modifier defined in the
- first place?
-
- Some speculations:
-
- int i;
- printf("%hd", i);
- printf("%d", (short)i);
-
- This question is stretching a bit to try to find what if anything the
- 'h' modifier is ever used for. Certainly the first line would have a
- different result without the 'h', but casting seems like it ought to
- have the same result.
-
-
- K&R2 is silent on whether "%hn" is valid as a conversion specifier. If
- so, clearly this is a case in which the 'h' is absolutely required to
- get the desired behavior.
-
- Thanks,
- michael j zehr
-